package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LC438 {

    public List<Integer> findAnagrams(String s, String p) {
        if (s.length() < p.length()) return new ArrayList<Integer>();
        int[] scount = new int[26];
        int[] pcount = new int[26];
        for (int i = 0; i < p.length(); i++) {
            scount[s.charAt(i) - 'a'] ++;
            pcount[p.charAt(i) - 'a'] ++;
        }
        ArrayList<Integer> ans = new ArrayList<>();
        if (Arrays.equals(scount,pcount)) ans.add(0);

        for (int i = 0; i + p.length() < s.length(); i++) {
            scount[s.charAt(i) -'a'] --;
            scount[s.charAt(i+p.length()) - 'a'] ++;
            if (Arrays.equals(scount,pcount)) ans.add(i+1);
        }
        return ans;
    }

}
